package com.yiqi.news.mvp.presenter

import android.app.Application

import com.jess.arms.integration.AppManager
import com.jess.arms.di.scope.ActivityScope
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.http.imageloader.ImageLoader
import com.yiqi.news.app.utils.ApiException
import com.yiqi.news.app.utils.RxUtils
import com.yiqi.news.app.utils.ValidatorUtil
import com.yiqi.news.entity.BaseResponse
import com.yiqi.news.entity.User
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import javax.inject.Inject

import com.yiqi.news.mvp.contract.LoginContract
import com.yiqi.news.mvp.model.api.HttpConstant
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/06/2019 13:47
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
class LoginPresenter
@Inject
constructor(model: LoginContract.Model, rootView: LoginContract.View) :
        BasePresenter<LoginContract.Model, LoginContract.View>(model, rootView) {
    @Inject
    lateinit var mErrorHandler: RxErrorHandler
    @Inject
    lateinit var mApplication: Application
    @Inject
    lateinit var mImageLoader: ImageLoader
    @Inject
    lateinit var mAppManager: AppManager


    fun sendCode(mobile: String) {
        if (!ValidatorUtil.isMobile(mobile)) {
            mRootView.showMobileError("手机号格式不正确")
            return
        }
        mModel.sendCode(mobile)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<User>>(mErrorHandler) {
                    override fun onNext(t: BaseResponse<User>) {
                        if (t.isSuccess) {
                            mRootView.showCodeSuccess()
                        } else {
                            mRootView.showMobileError("手机号格式不正确")
                        }

                    }
                })
    }

    fun login(mobile: String, pwd: String) {
        if (!ValidatorUtil.isMobile(mobile)) {
            mRootView.showMobileError("手机号格式不正确")
            return
        }

        if (pwd.isNullOrEmpty()) {
            mRootView.showMobileError("请输入验证码")
            return
        }

        if (pwd.length < 6) {
            mRootView.showLoginError("验证码格式错误")
            return
        }
        mModel.login(mobile, pwd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<User>>(mErrorHandler) {
                    override fun onNext(t: BaseResponse<User>) {

                        if (t.isSuccess) {
                            mRootView.showLoginSuccess(t.data)
                        }
                    }

                    override fun onError(t: Throwable) {
//                        super.onError(t)
                        val apiException = t as ApiException
                        if (t.code == HttpConstant.LOGIN_NOT_BIND) {
                            mRootView.showUnBind()
                        } else {
                            mRootView.showLoginError(t.message.toString())
                        }
                    }


                })
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}
